import numpy as np
import csv


def load_data(file_path):
    """
    Load repository data and calculate activity metrics
    Columns: repo_name,owner,stars,forks,language,created_at,last_commit,description
    Return: 2D NumPy array of shape (repos, 3) containing [stars, forks, active_days]
    """
    with open(file_path, 'r') as file:
        reader = csv.reader(file)
        headers = next(reader)  # Skip header
        data = list(reader)

    stars = np.array([int(row[2]) for row in data])
    forks = np.array([int(row[3]) for row in data])
    created_at = np.array([np.datetime64(row[5]) for row in data])
    last_commit = np.array([np.datetime64(row[6]) for row in data])
    active_days = (last_commit - created_at).astype('timedelta64[D]').astype(int)

    return np.column_stack((stars, forks, active_days))


def calculate_statistics(data):
    """
    Calculate repository metrics statistics
    Return: Dictionary containing {
        'means': [stars_mean, forks_mean, days_mean],
        'medians': [stars_median, forks_median, days_median],
        'variances': [stars_var, forks_var, days_var],
        'stds': [stars_std, forks_std, days_std]
    }
    """
    means = np.mean(data, axis=0)
    medians = np.median(data, axis=0)
    variances = np.var(data, axis=0)
    stds = np.std(data, axis=0)

    return {
        'means': means,
        'medians': medians,
        'variances': variances,
        'stds': stds
    }


def print_results(stats):
    """
    Print formatted results with proper indentation
    """
    metrics = ['Stars', 'Forks', 'Active Days']
    for metric, mean, med, var, std in zip(metrics,
                                           stats['means'],
                                           stats['medians'],
                                           stats['variances'],
                                           stats['stds']):
        print(f"{metric}:")
        print(f"    Average: {mean:.1f}")
        print(f"    Median: {med:.1f}")
        print(f"    Variance: {var:.1f}")
        print(f"    Standard Deviation: {std:.1f}")


repo_data = load_data('china-repos.csv')
stats = calculate_statistics(repo_data)
print_results(stats)
# hisdjifhjsdhf